<html>
<head><meta charset="utf-8"><title>safe reference conversion to `repr(transparent)` newtype? · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F.html">safe reference conversion to `repr(transparent)` newtype?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="222944357"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe%20reference%20conversion%20to%20%60repr%28transparent%29%60%20newtype%3F/near/222944357" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tony Arcieri <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F.html#222944357">(Jan 16 2021 at 00:18)</a>:</h4>
<p>LazyZulip request:</p>
<p>Is there a tracking issue for doing a safe conversion (e.g. cast) from a reference of the inner type a <code>#[repr(transparent)]</code> contains to a reference to the outer type?</p>



<a name="222945098"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe%20reference%20conversion%20to%20%60repr%28transparent%29%60%20newtype%3F/near/222945098" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Quy Nguyen <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F.html#222945098">(Jan 16 2021 at 00:29)</a>:</h4>
<p>That is probably just the safe trasnmute working group.</p>



<a name="222946109"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe%20reference%20conversion%20to%20%60repr%28transparent%29%60%20newtype%3F/near/222946109" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tony Arcieri <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F.html#222946109">(Jan 16 2021 at 00:43)</a>:</h4>
<p>oh right, and now I remember going down this path before</p>



<a name="222946212"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe%20reference%20conversion%20to%20%60repr%28transparent%29%60%20newtype%3F/near/222946212" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F.html#222946212">(Jan 16 2021 at 00:44)</a>:</h4>
<p>that doesn't sound safe in all cases, well, it is not unsound from the language level perspective, but it could be library-level unsafe.</p>



<a name="222946252"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe%20reference%20conversion%20to%20%60repr%28transparent%29%60%20newtype%3F/near/222946252" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F.html#222946252">(Jan 16 2021 at 00:45)</a>:</h4>
<p>yeah, types will have to opt into safe transmutation</p>



<a name="222946260"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe%20reference%20conversion%20to%20%60repr%28transparent%29%60%20newtype%3F/near/222946260" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F.html#222946260">(Jan 16 2021 at 00:45)</a>:</h4>
<p>as in, if <code>Foo</code> ensures invariants in its <code>new(i: Inner)</code> method and then uses unsafe in its other methods relying on said invariants, then the...</p>



<a name="222947201"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe%20reference%20conversion%20to%20%60repr%28transparent%29%60%20newtype%3F/near/222947201" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F.html#222947201">(Jan 16 2021 at 00:59)</a>:</h4>
<p><span class="user-mention silent" data-user-id="123586">nagisa</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F/near/222946212">said</a>:</p>
<blockquote>
<p>that doesn't sound safe in all cases, well, it is not unsound from the language level perspective, but it could be library-level unsafe.</p>
</blockquote>
<p>Depends on what <code>repr(transparent)</code> newtype you are talking about.  <code>&amp;mut T</code>=&gt;<code>&amp;mut MaybeUninit&lt;T&gt;</code> is unsound, as is <code>&amp;mut NonZeroU8</code>=&gt;<code>&amp;mut u8</code>, at the language level (IE. can result in lang-level UB in the absense of any other unsafe code (including within other libraries)).</p>



<a name="222952342"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe%20reference%20conversion%20to%20%60repr%28transparent%29%60%20newtype%3F/near/222952342" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F.html#222952342">(Jan 16 2021 at 02:19)</a>:</h4>
<p>niches are a problem too, like <code>NonZeroU32</code> is a transparent wrapper of <code>u32</code></p>



<a name="222952405"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe%20reference%20conversion%20to%20%60repr%28transparent%29%60%20newtype%3F/near/222952405" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F.html#222952405">(Jan 16 2021 at 02:20)</a>:</h4>
<p>e.g. <code>&amp;0u32</code> into <code>&amp;NonZeroU32</code>, then deref copy, would give you a zero <code>NonZero32</code></p>



<a name="222952427"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe%20reference%20conversion%20to%20%60repr%28transparent%29%60%20newtype%3F/near/222952427" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F.html#222952427">(Jan 16 2021 at 02:21)</a>:</h4>
<p>that's kind of what <span class="user-mention" data-user-id="257758">@Connor Horman</span> was saying, but mutability is not the only issue</p>



<a name="222952500"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe%20reference%20conversion%20to%20%60repr%28transparent%29%60%20newtype%3F/near/222952500" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F.html#222952500">(Jan 16 2021 at 02:22)</a>:</h4>
<p>Indeed.</p>



<a name="222952516"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe%20reference%20conversion%20to%20%60repr%28transparent%29%60%20newtype%3F/near/222952516" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F.html#222952516">(Jan 16 2021 at 02:23)</a>:</h4>
<p>(Though mutability makes both directions unsound)</p>



<a name="222952531"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe%20reference%20conversion%20to%20%60repr%28transparent%29%60%20newtype%3F/near/222952531" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F.html#222952531">(Jan 16 2021 at 02:23)</a>:</h4>
<p>Because you can soundly go <code>&amp;NonZeroU32</code>=&gt;<code>&amp;u32</code>.</p>



<a name="222952586"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe%20reference%20conversion%20to%20%60repr%28transparent%29%60%20newtype%3F/near/222952586" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F.html#222952586">(Jan 16 2021 at 02:24)</a>:</h4>
<p>only in the absence of <code>UnsafeCell</code>?</p>



<a name="222952601"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe%20reference%20conversion%20to%20%60repr%28transparent%29%60%20newtype%3F/near/222952601" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F.html#222952601">(Jan 16 2021 at 02:25)</a>:</h4>
<p>Well, That's why I mentioned those types explicitly.</p>



<a name="222952610"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe%20reference%20conversion%20to%20%60repr%28transparent%29%60%20newtype%3F/near/222952610" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F.html#222952610">(Jan 16 2021 at 02:25)</a>:</h4>
<p>yeah, ok</p>



<a name="222952627"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe%20reference%20conversion%20to%20%60repr%28transparent%29%60%20newtype%3F/near/222952627" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F.html#222952627">(Jan 16 2021 at 02:26)</a>:</h4>
<p>interior mutability is still mutability</p>



<a name="222952673"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe%20reference%20conversion%20to%20%60repr%28transparent%29%60%20newtype%3F/near/222952673" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F.html#222952673">(Jan 16 2021 at 02:26)</a>:</h4>
<p>Also, UnsafeCell is a good point. It is never sound go from &amp;T to &amp;Cell&lt;T&gt;, which is transparent of UnsafeCell&lt;T&gt; and thus T.</p>



<a name="222952709"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe%20reference%20conversion%20to%20%60repr%28transparent%29%60%20newtype%3F/near/222952709" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F.html#222952709">(Jan 16 2021 at 02:27)</a>:</h4>
<p>(It isn't inherently unsound I don't believe to go from &amp;T to &amp;UnsafeCell&lt;T&gt;, though it could be immediate UB from the SharedReadOnly=&gt;SharedReadWrite)</p>



<a name="222962231"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe%20reference%20conversion%20to%20%60repr%28transparent%29%60%20newtype%3F/near/222962231" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F.html#222962231">(Jan 16 2021 at 05:43)</a>:</h4>
<p>the <code>bytemuck</code> crate has a trait for this sort of thing</p>



<a name="222974752"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe%20reference%20conversion%20to%20%60repr%28transparent%29%60%20newtype%3F/near/222974752" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F.html#222974752">(Jan 16 2021 at 10:53)</a>:</h4>
<p>There is also <a href="https://docs.rs/ref-cast">https://docs.rs/ref-cast</a></p>



<a name="223072407"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe%20reference%20conversion%20to%20%60repr%28transparent%29%60%20newtype%3F/near/223072407" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tony Arcieri <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F.html#223072407">(Jan 18 2021 at 00:50)</a>:</h4>
<p>So for the cases I was thinking of, this could be a casting operation the <code>impl NewType</code> does to itself, but not otherwise available elsewhere</p>



<a name="223072413"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe%20reference%20conversion%20to%20%60repr%28transparent%29%60%20newtype%3F/near/223072413" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tony Arcieri <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F.html#223072413">(Jan 18 2021 at 00:50)</a>:</h4>
<p>that'd respect any invariants of the newtype, although not necessarily things like <code>UnsafeCell</code></p>



<a name="223072420"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe%20reference%20conversion%20to%20%60repr%28transparent%29%60%20newtype%3F/near/223072420" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tony Arcieri <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F.html#223072420">(Jan 18 2021 at 00:51)</a>:</h4>
<p>but uhh, a particular use case would be <code>impl Index&lt;...&gt; for NewType</code></p>



<a name="223133962"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe%20reference%20conversion%20to%20%60repr%28transparent%29%60%20newtype%3F/near/223133962" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/safe.20reference.20conversion.20to.20.60repr(transparent).60.20newtype.3F.html#223133962">(Jan 18 2021 at 15:29)</a>:</h4>
<p>If the invariants of both are satisified, it is sound to transmute from a reference to <code>T</code> to the same kind of reference to a repr(transparent) wrapper arround <code>T</code>.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>